package com.example.spingjpajson.controller;

import com.example.spingjpajson.entity.DepartmentEntity;
import com.example.spingjpajson.entity.json.DepartmentExtraJson;
import com.example.spingjpajson.repository.DepartmentRepository;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
public class IndexController {
    @Autowired
    DepartmentRepository departmentRepository;

    @GetMapping("/")
    public Page<DepartmentEntity> index(String type) {
        int page = 0;
        // 每页条数
        int pageSize = 20;
        log.info("page={}", page);
        log.info("pageSize={}", pageSize);
        //排序
        Sort sort = Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime"));
        //分页
        Pageable pageable = PageRequest.of(page, pageSize, sort);
        ///////////////
        Specification<DepartmentEntity> specification = (root, criteriaQuery, cb) -> {
            List<Predicate> list = Lists.newArrayList();
            if (StringUtils.hasLength(type)) {
                Expression<String> findInSetFun = cb.function("JSON_EXTRACT", String.class, root.get("extra"), cb.literal("$.type"));
                Expression<String> test2 = cb.function("JSON_ARRAY", String.class, cb.literal("test"));
                Expression<String> findInSetFun2 = cb.function("JSON_CONTAINS", String.class, findInSetFun, test2);
                //设置条件 只要返回值 type 则说明该参数存在于目标字符串中
                list.add(cb.notEqual(findInSetFun2, "0"));
            }
            return cb.and(list.toArray(new Predicate[list.size()]));
        };


        //////
        Page<DepartmentEntity> all = departmentRepository.findAll(specification, pageable);
        log.info("{}", all);
        log.info("getTotalElements={}", all.getTotalElements());
        log.info("getTotalPages={}", all.getTotalPages());
        log.info("getSize={}", all.getSize());
        if (all.getTotalElements() > 0) {
            log.info("getContent[0]={}", all.getContent().get(0));
            //
            DepartmentEntity departmentEntity = all.getContent().get(0);
            log.info("departmentEntity={}", departmentEntity);
            log.info("departmentEntity.getExtra={}", departmentEntity.getExtra());
            log.info("getType={}", departmentEntity.getExtra().getType());
        }
        return all;
    }

    @GetMapping("/add")
    public DepartmentEntity add() {
        DepartmentEntity item = new DepartmentEntity();
        item.setId(UUID.randomUUID().toString());
        //
        DepartmentExtraJson json = new DepartmentExtraJson();
        List<String> ids = new ArrayList<>();
        ids.add(item.getId());
        ids.add("test");
        json.setType(ids);
        item.setExtra(json);
        item.setCreateTime(LocalDateTime.now());
        departmentRepository.save(item);
        return item;
    }

}
